package com.hitqz.robot.daemon.quartz.schedule;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.entity.GoodsBuffersEntity;
import com.hitqz.robot.api.business.entity.JobCachesEntity;
import com.hitqz.robot.biz.service.GoodsBuffersService;
import com.hitqz.robot.biz.service.JobCachesService;
import com.hitqz.robot.biz.service.RobotWarningRecordService;
import com.hitqz.robot.daemon.quartz.constants.RobotQuartzEnum;
import com.hitqz.robot.daemon.quartz.util.MixPosTaskUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author xupkun
 * @date 2025/4/11
 */
@Component("YzjPosTaskSchedule")
@RequiredArgsConstructor
@Slf4j
public class YzjPosTaskSchedule {

    private final RestTemplate restTemplate;

    private final GoodsBuffersService goodsBuffersService;

    private final JobCachesService jobCachesService;

    private final RobotWarningRecordService robotWarningRecordService;

    //String uri = "http://192.10.13.20:9999/lzms/getJjwXx?jjw_sbbh=22N07&agv_sbbh=5409";


    /**
     * 京天威 定时任务请求接口 交接位置的信息
     * @return
     */
    public String execute() {
        String src = "22N07";
        String result = "";
        try {
            GoodsBuffersEntity goodsBuffersEntity = goodsBuffersService.getOne(Wrappers.<GoodsBuffersEntity>lambdaUpdate()
                    .eq(GoodsBuffersEntity::getStoreName, src).last("limit 1"));
            if (goodsBuffersEntity != null) {
                if (goodsBuffersEntity.getStoreStatus() == 0){
                    result = restTemplate.getForObject("http://192.10.13.20:9999/lzms/getJjwXx?jjw_sbbh=22N07&agv_sbbh=5409", String.class);
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    if (jsonObject.containsKey("data")) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        if (data.containsKey("pjid")) {
                            String pjid = data.getString("pjid");
                            String lastPjId = MixPosTaskUtil.taskPjId.get(src);
                            if (StrUtil.isEmpty(lastPjId) || !pjid.trim().equals(lastPjId.trim())) {
                                String des = data.getString("des");
                                String taskUri = "http://192.10.11.153:9999/admin/km/agv/call?src="
                                        + src + "&des=" + des + "&pjid=" + pjid + "&pjid1=" + pjid;
                                String taskName = createTaskName(src, des, true);
                                JobCachesEntity jobCachesEntity = jobCachesService.getOne(Wrappers.<JobCachesEntity>lambdaQuery()
                                        .eq(JobCachesEntity::getTaskName, taskName)
                                        .last("limit 1")
                                );
                                if (jobCachesEntity == null) {
                                    // 查询目标点的设备状态
                                    String uri = "http://192.10.13.20:9999/sb/getSBZT?sbbh=" + des;
                                    String sbzt = restTemplate.getForObject(uri, String.class);
                                    JSONObject sbztJsonObject = JSONObject.parseObject(sbzt);
                                    if (sbztJsonObject.containsKey("data") && "3".equals(sbztJsonObject.getString("data"))) {
                                        goodsBuffersEntity.setStoreStatus(2);
                                        goodsBuffersService.updateById(goodsBuffersEntity);
                                        MixPosTaskUtil.taskPjId.put(src, pjid);
                                        String taskResult = restTemplate.postForObject(taskUri, "", String.class);
                                        log.info("交接位置任务请求地址: {}--{}- pjid: {}- lastpjid: {}", taskUri, taskResult, pjid, lastPjId);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.info("{} 请求交接位置失败结果: {} 失败:{}",src,  result, e.getMessage());
        }
        return RobotQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }


    public String createTaskName(String src, String des, boolean returnTaskName) {
        String taskName = "";
        String uri = "http://ip:9999/admin/km/agv/call/dispatch?src=" + src + "&des=" + des + "&groupId=executeGroupId";
        if (src.equals("22N07") && des.equals("30S01")) {
            taskName =  "镟修-压装-70T";
            uri = uri.replace("ip", "192.10.11.154").replace("executeGroupId", "6");
        } else if (src.equals("22N07") && des.equals("30N01")) {
            taskName =  "镟修-压装-60T";
            uri = uri.replace("ip", "192.10.11.154").replace("executeGroupId", "6");
        }
        return returnTaskName ? taskName : uri;
    }



}
